﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class Rebate :BasePage
    {
        protected int totalRecords = 0;
        protected int totalPages = 1;
        IWeekLongBLL weekLongBLL = IocContainer.Instance.Resolve<IWeekLongBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            //SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                //ControlAuthority(new List<WebControl> { btnAdd, btnDelete, btnSearch }, 20);
            }
            Query();
        }


        protected void Query()
        {
            int pageIndex = 0;
            int.TryParse(this.pageNum.Value, out pageIndex);
            int pagePerSize = 0;
            int.TryParse(this.numPerPageSelect.Value, out pagePerSize);
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(this.txtCustomerName.Text))
            {
                strWhere.Append(" and FName like '%" + this.txtCustomerName.Text + "%'");
            }
            if (this.ddlStatus.SelectedValue != "-1")
            {
                strWhere.Append(" and FisYes=" + this.ddlStatus.SelectedValue);
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderField.Value) ? " " + this.orderField.Value : "  FName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderValue.Value) ? " " + this.orderValue.Value : "  asc ");

            DataTable dtc = weekLongBLL.GetRebateByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);
            this.EmpTableBody.Text = BuildEmployeeTable(dtc);
        }

        protected string BuildEmployeeTable(DataTable dtEmployee)
        {
            StringBuilder result = new StringBuilder();
            if (dtEmployee != null && dtEmployee.Rows.Count > 0)
            {
                int rowNum = 1;
                foreach (DataRow row in dtEmployee.Rows)
                {
                    result.Append(rowNum % 2 == 0 ? "<tr class='trbg' rel='" + row["Fitemid"] + "' rel2='" + row["Fcustid"] + "' rel3='" + row["Famount"] + "'>" : "<tr rel='" + row["Fitemid"] + "'  rel2='" + row["Fcustid"] + "'  rel3='" + row["Famount"] + "'>");
                    result.AppendFormat("<td>{0}</td>", row["FcustTypeName"]);
                    result.AppendFormat("<td>{0}</td>", row["FName"]);
                    result.AppendFormat("<td>{0}</td>", row["FRebateDate"]);
                    result.AppendFormat("<td>{0}</td>", row["Fnode"]);
                    result.AppendFormat("<td>{0}</td>", row["Famount"]);
                    result.AppendFormat("<td>{0}</td>", row["FisYesName"]);
                    result.Append("</tr>");
                    rowNum++;
                }
            }
            return result.ToString();

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.pageNum.Value = "1";
            Query();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                weekLongBLL.DeleteRebate(int.Parse(this.selectedEmpID.Value), int.Parse(this.selectedCustID.Value), decimal.Parse(selectedFamount.Value));

                Query();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
